package com.ant.storage.service.impl;

import com.ant.storage.dto.CompanyDto;
import com.ant.storage.mapper.*;
import com.ant.storage.pojo.*;
import com.ant.storage.service.ICompanyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzd
 * @since 2025-10-23
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    MaterialMapper materialMapper;

    @Autowired
    MaterialProductMapper materialProductMapper;


    @Autowired
    OrganicMapper organicMapper;

    @Autowired
    OrganicProductMapper organicProductMapper;


    @Override
    @Transactional
    public Company saveCompany(CompanyDto dto) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("user_name",dto.getContactTel().trim());
        User one = userMapper.selectOne(wrapper);
        if(one!=null){
            Company companies = baseMapper.selectOne(new QueryWrapper<Company>().eq("user_id", one.getId()));
            return  companies;
        }
        User user=new User();
        user.setUserName(dto.getContactTel());
        user.setUserPwd("000000");
        user.setReview(1);
        user.setCreationTime(LocalDateTime.now());
        user.setAccountType(2);
        user.setIsOpen(0);
        userMapper.insert(user);
        Company companys=new Company();
        companys.setUserId(user.getId());
        companys.setProvince("四川省");
        companys.setCity(dto.getCity());
        companys.setCounty(dto.getCounty());
        companys.setAddress(dto.getAddress());
        companys.setCompanyCode(dto.getCompanyCode());
        companys.setCompanyName(dto.getCompanyName());
        companys.setBusinessUrl(dto.getBusinessUrl());
        companys.setPersonName(dto.getPersonName());
        companys.setPersonUrl(dto.getPersonUrl());
        companys.setContact(dto.getContact());
        companys.setContactTel(dto.getContactTel());
        companys.setEmail(dto.getEmail());
        companys.setCreationTime(LocalDateTime.now());
        baseMapper.insert(companys);
        return companys;
    }

    @Override
    public Company findCompanyOrganicById(Integer id) {
        Company company = baseMapper.selectOne(new QueryWrapper<Company>().eq("id", id));
        List<Organic> materials = organicMapper.selectList(new QueryWrapper<Organic>()
                .eq("company_id", id));
        materials.stream().forEach(itme->{
            List<OrganicProduct> parentList= organicProductMapper.selectList(new QueryWrapper<OrganicProduct>()
                    .eq("parent_id", itme.getId()));
            itme.setProductList(parentList);
        });
        company.setOrganicList(materials);
        return company;
    }

    @Override
    public Company findCompanyMaterialById(Integer id) {
        Company company = baseMapper.selectOne(new QueryWrapper<Company>().eq("id", id));
        List<Material> materials = materialMapper.selectList(new QueryWrapper<Material>()
                .eq("company_id", id));
        materials.stream().forEach(itme->{
            List<MaterialProduct> parentList= materialProductMapper.selectList(new QueryWrapper<MaterialProduct>()
                    .eq("parent_id", itme.getId()));
            itme.setProductList(parentList);
        });
        company.setMaterialList(materials);
        return company;
    }
}
